﻿using CPPEI.Coolzon.Service.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CPPEI.Coolzon.Data.MainServiceData;
using CPPEI.Coolzon.Common;

namespace CPPEI.Coolzon.Server.ServiceImplement
{
    public class ReqGetViewStateHandler : BaseHandler<ReqGetViewStateData, RespGetViewStateData>
    {
        public ReqGetViewStateHandler()
            : base(SCENARIO.CONNECT, COMMAND.CONNECT.GETVIEWSTATE)
        {
        }

        protected override RespGetViewStateData OnHandle(Request request, ReqGetViewStateData requestData)
        {
            Logger.DebugToTag("MainService", string.Format("终端请求获取服务方案:{0} 的视图状态状态,DomainID：{1}", requestData.SolutionName, request.Header.DomainId));
            CacheData.ControlDomain domain = CacheData.ServerData.Instance.GetDomain(request.Header.DomainId);
            if (domain != null)
            {
                if (!string.Equals(CZEnvironment.Instance.ServerIP, requestData.MainServerIp) || CZEnvironment.Instance.ServerPort != requestData.MainServerPort)
                {
                    Common.Logger.InfoToTag("MainService", "域{0},获取方案:{1} 的视图状态失败,不是主服务",
                        domain.Id, requestData.SolutionName);
                    return null;
                }
                CacheData.Client client = domain.GetClient(request.Header.UserIdentity);
                if (client != null)
                {
                    RespGetViewStateData response = new RespGetViewStateData();
                    response.ServerIp = CZEnvironment.Instance.ServerIP;
                    response.ServerPort = CZEnvironment.Instance.ServerPort;
                    if (string.Equals(requestData.SolutionName, domain.SolutionName))
                    {
                        if (domain.ViewStateAge > 0 && !string.IsNullOrWhiteSpace(domain.ViewStateToken))
                        {
                            response.HasViewState = true;
                            response.ViewStateToken = domain.ViewStateToken;
                            Logger.InfoToTag("MainService", string.Format("终端请求获取服务方案:{0} 的视图状态状态成功,Age:{1},Token:{2},DomainID：{3}", requestData.SolutionName, domain.ViewStateAge, domain.ViewStateToken, request.Header.DomainId));
                        }
                        else
                        {
                            response.HasViewState = false;
                            response.ViewStateToken = string.Empty;
                            Logger.InfoToTag("MainService", string.Format("终端请求获取服务方案:{0} 的视图状态状态失败,服务器无状态,Age:{1},Token:{2},DomainID：{3}", requestData.SolutionName, domain.ViewStateAge, domain.ViewStateToken, request.Header.DomainId));
                        }
                    }
                    else
                    {
                        response.HasViewState = false;
                        response.ViewStateToken = string.Empty;
                        Logger.InfoToTag("MainService", string.Format("终端请求获取服务方案:{0} 的视图状态状态失败,与服务器方案:{1} 不同,DomainID：{2}", requestData.SolutionName, domain.SolutionName, request.Header.DomainId));
                    }
                    return response;
                }
                else
                {
                    Common.Logger.ErrorToTag("MainService", "域{0},用户{1}获取方案:{2} 的视图状态失败,client不存在",
                        domain.Id,
                        request.Header.UserIdentity,
                        requestData.SolutionName);
                }
            }
            else
            {
                Common.Logger.ErrorToTag("MainService", "域{0},用户{1}获取方案:{2} 的视图状态失败,域不存在",
                    request.Header.DomainId,
                    request.Header.UserIdentity,
                    requestData.SolutionName);
            }
            return null;
        }
    }
}
